import numpy as np


def f_坐标转索引(维度, coord):
	index = (维度[0] * 维度[1]) * int(coord[2])
	index += 维度[0]*int(coord[1])
	index += int(coord[0])
	#print('变换后坐标 == ', index, xyz, 域大小*0.5 + coord)
	return index

def f_物体坐标转域索引坐标(coord, 比例, 偏移):
	#print('------------ coord ---- ', coord, 偏移, (coord - 偏移))
	xyz = (coord - 偏移) #- 比例*0.5 
	xyz /= 比例
	return (int(xyz[0]), int(xyz[1]), int(xyz[2]))

def f_索引转坐标(维度, index):
	#print('查找索引 = ', 查找索引)
	切片大小 = 维度[0]*维度[1]
	z = int(index / 切片大小)
	所在切片数量 = (index - int(z * 切片大小))
	y = int( 所在切片数量 / 维度[0] )
	x = 所在切片数量 - y*维度[0]

	return (x,y,z, 切片大小)


def f_get图数据(图, 坐标):
	#print('图[坐标[2]][坐标[1]][坐标[0]] = ', 图[坐标[2]][坐标[1]][坐标[0]], 坐标)
	return 图[坐标[2]][坐标[1]][坐标[0]]

def f_遮罩查询(坐标, 遮罩):
	if len(坐标):
		return 遮罩[坐标[2]][坐标[1]][坐标[0]]
	return -127

def f_set遮罩(坐标, 遮罩, value):
	遮罩[坐标[2]][坐标[1]][坐标[0]] = value

def f_set遮罩累加(坐标, 遮罩, value):
	遮罩[坐标[2]][坐标[1]][坐标[0]] += value
def f_set遮罩递减(坐标, 遮罩, value):
	遮罩[坐标[2]][坐标[1]][坐标[0]] -= value


def f_域单元采样_6X(维度, 中点坐标):
	#坐标 = f_索引转坐标(维度, 中点索引)
	X = 中点坐标[0]
	Y = 中点坐标[1]
	Z = 中点坐标[2]
	#W = 坐标[3]
	#index = 0

	ID = []
	#ID[0] = 中点索引
	for Offset in [-1, 1]:
		#print('Offset = ', Offset)
		x坐标 = X+Offset
		#index += 1
		if x坐标 >= 0 and x坐标 < 维度[0] :
			ID.append((x坐标, Y, Z))
		else:
			ID.append(())
			#print('X_6X = ', 'w=', W, ' x=', x坐标, ' y=', Y, ' z=', Z)
		y坐标 = Y+Offset
		#index += 1
		if y坐标 >= 0 and y坐标 < 维度[1] :
			ID.append((X, y坐标, Z))
		else:
			ID.append(())
			#print('Y_6X = ', 'w=', W, ' x=', X, ' y=', y坐标, ' z=', Z)
		z坐标 = Z+Offset
		#index += 1
		if z坐标 >= 0 and z坐标 < 维度[2] :
			ID.append((X, Y, z坐标))
		else:
			ID.append(())
			#print('Z_6X = ', 'w=', W, ' x=', X, ' y=', Y, ' z=', z坐标)
	#print('f_域单元采样_6X = ', ID, '中点索引 = ', 中点索引, 'xyz=', X,Y,Z)
	return ID


def f_域单元采样_3X3(维度, 中点坐标, 遮罩):
	X = 中点坐标[0]
	Y = 中点坐标[1]
	Z = 中点坐标[2]

	GRIDs = []
	for ZOffset in range(-1, 2):
		z坐标 = Z+ZOffset
		if z坐标 < 0 or z坐标 >= 维度[2] :
			z坐标 = -1

		for YOffset in range(-1, 2):
			y坐标 = Y+YOffset
			if y坐标 < 0 or y坐标 >= 维度[1] :
				y坐标 = -1

			for XOffset in range(-1, 2):
				x坐标 = X+XOffset
				if x坐标 < 0 or x坐标 >= 维度[0] :
					x坐标 = -1

				coord = (x坐标, y坐标, z坐标)
				#print('coord[0] * coord[1] * coord[2] = ', coord[0], coord[1], coord[2]) #f_遮罩查询(coord, 遮罩) != 127
				if coord[0] < 0 or coord[1] < 0 or coord[2] < 0 :
					GRIDs.append(())
				else :
					GRIDs.append(coord)
					
	return GRIDs












def f_域单元采样_5X5(维度, 中点坐标, 遮罩):
	X = 中点坐标[0]
	Y = 中点坐标[1]
	Z = 中点坐标[2]

	GRIDs = []
	for ZOffset in range(-2, 3):
		z坐标 = Z+ZOffset
		if z坐标 < 0 or z坐标 >= 维度[2] :
			z坐标 = -1

		for YOffset in range(-2, 3):
			y坐标 = Y+YOffset
			if y坐标 < 0 or y坐标 >= 维度[1] :
				y坐标 = -1

			for XOffset in range(-2, 3):
				x坐标 = X+XOffset
				if x坐标 < 0 or x坐标 >= 维度[0] :
					x坐标 = -1
					
				coord = (x坐标, y坐标, z坐标)
				#print('coord[0] * coord[1] * coord[2] = ', coord[0], coord[1], coord[2]) #f_遮罩查询(coord, 遮罩) != 127
				if coord[0] < 0 or coord[1] < 0 or coord[2] < 0 :
					GRIDs.append(())
				else :
					GRIDs.append(coord)
					
	return GRIDs













def f_体素应用遮挡(GRIDs, mask):
	newGRIDs = []
	maskNum = len(mask)
	for i in GRIDs:
		id = -1
		#print('mask = ', i, GRIDs)
		if i < maskNum and bool(mask[i]) :
			id = i
		newGRIDs.append(id)
	return newGRIDs



def f_查找体素阻塞方向6X(GRIDs, 查找轴s, 遮罩):
	邻接物体索引 = None
	#print('findIndex = ', Indexs, findIndex)
	#length = len(GRIDs)
	#物体索引 = -1
	轴方向 = -1
	轴下标 = -1
	有无找到 = False
	
	#print('查找轴s = ', 查找轴s)
	for i in 查找轴s:
		#if bool(mask[ GRIDs[i+1] ]) == False and bool(mask[ GRIDs[i+4] ]) == False : continue
		#print('f_遮罩查询(GRIDs[i], 遮罩) = ', f_遮罩查询(GRIDs[i], 遮罩), f_遮罩查询(GRIDs[i+3], 遮罩))
		m1 = f_遮罩查询(GRIDs[i], 遮罩)
		m2 = f_遮罩查询(GRIDs[i+3], 遮罩)
		#print('f_遮罩查询(GRIDs[i], 遮罩) = ', m1, m2)
		轴下标 = i
		if m1 < 0 and (m2 == 0 or m2 == 127):
			邻接物体索引 = GRIDs[i+3]
			轴方向 = 1
			有无找到 = True
			break
		elif (m1 == 0 or m1 == 127) and m2 < 0:
			邻接物体索引 = GRIDs[i]
			轴方向 = -1
			有无找到 = True
			break

	#print('邻接物体索引 = ', 邻接物体索引)
	return 轴方向, 轴下标, 邻接物体索引, 有无找到









def f_查找体素任意阻塞方向6X(GRIDs, 查找轴s, 遮罩):
	邻接物体索引 = None
	
	轴方向 = -1
	轴下标 = -1
	有无找到 = False
	
	#print('查找轴s = ', 查找轴s)
	for i in 查找轴s:
		
		m1 = f_遮罩查询(GRIDs[i], 遮罩)
		m2 = f_遮罩查询(GRIDs[i+3], 遮罩)
		#print('f_遮罩查询(GRIDs[i], 遮罩) = ', m1, m2)
		轴下标 = i
		if m1 < 0:
			邻接物体索引 = GRIDs[i+3]
			轴方向 = 1
			有无找到 = True
			break
		elif m1 == 0:
			邻接物体索引 = GRIDs[i]
			轴方向 = -1
			有无找到 = True
			break
	#print('邻接物体索引 = ', 邻接物体索引)
	return 轴方向, 轴下标, 邻接物体索引, 有无找到






def f_暂时冻结坐标对象(GRIDs, 遮罩):
	for i in GRIDs:
		if f_遮罩查询(i, 遮罩) == 0:
			#f_set遮罩累加(i, 遮罩, 1)
			f_set遮罩(i, 遮罩, 1)
	

def f_解冻结坐标对象(GRIDs, 遮罩):
	for coord in GRIDs:
		mask = f_遮罩查询(coord, 遮罩)
		if mask == 1:
			#f_set遮罩递减(coord, 遮罩, 1)
			f_set遮罩(coord, 遮罩, 0)


def f_暂时冻结坐标对象3X3(GRIDs, 轴, 向, 遮罩):
	for i in range(6):
		if f_遮罩查询(GRIDs[i], 遮罩) >= 0:
			f_set遮罩累加(GRIDs[i], 遮罩, 1)


	
'''return
	if 轴 == 0 :
		if 向 > 0 :
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: f_set遮罩累加(GRIDs[i], 遮罩, 1) # and i != 0
		else:
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: f_set遮罩累加(GRIDs[i], 遮罩, 1) # and i != 3
			
	elif 轴 == 1 :
		if 向 > 0 :
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: # and i != 1
					f_set遮罩累加(GRIDs[i], 遮罩, 1)
		else:
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: # and i != 4
					f_set遮罩累加(GRIDs[i], 遮罩, 1)

	elif 轴 == 2 :
		if 向 > 0 :
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: # and i != 2
					f_set遮罩累加(GRIDs[i], 遮罩, 1)
		else:
			for i in range(6):
				if f_遮罩查询(GRIDs[i], 遮罩) >= 0: # and i != 5
					f_set遮罩累加(GRIDs[i], 遮罩, 1)'''